package com.vehiclemanagement.controller;

import com.vehiclemanagement.common.result.Result;
import com.vehiclemanagement.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 报表分析控制器
 * 处理各类统计报表和数据分析请求
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 获取车辆使用统计数据
     */
    @GetMapping("/vehicle/usage")
    public Result<List<Map<String, Object>>> getVehicleUsageStatistics(
            @RequestParam(required = false) Long vehicleId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        log.info("获取车辆使用统计数据: vehicleId={}, startTime={}, endTime={}", vehicleId, startTime, endTime);
        
        // 如果未提供时间范围，默认查询最近一年的数据
        if (startTime == null) {
            startTime = LocalDateTime.now().minusYears(1);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        try {
            List<Map<String, Object>> statistics = reportService.getVehicleUsageStatistics(vehicleId, startTime, endTime);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取车辆使用统计数据失败", e);
            return Result.error("获取车辆使用统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取车辆费用统计数据
     */
    @GetMapping("/vehicle/cost")
    public Result<List<Map<String, Object>>> getVehicleCostStatistics(
            @RequestParam(required = false) Long vehicleId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        log.info("获取车辆费用统计数据: vehicleId={}, startTime={}, endTime={}", vehicleId, startTime, endTime);
        
        // 如果未提供时间范围，默认查询最近一年的数据
        if (startTime == null) {
            startTime = LocalDateTime.now().minusYears(1);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        try {
            List<Map<String, Object>> statistics = reportService.getVehicleCostStatistics(vehicleId, startTime, endTime);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取车辆费用统计数据失败", e);
            return Result.error("获取车辆费用统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取驾驶员工作统计数据
     */
    @GetMapping("/driver/work")
    public Result<List<Map<String, Object>>> getDriverWorkStatistics(
            @RequestParam(required = false) Long driverId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        log.info("获取驾驶员工作统计数据: driverId={}, startTime={}, endTime={}", driverId, startTime, endTime);
        
        // 如果未提供时间范围，默认查询最近一年的数据
        if (startTime == null) {
            startTime = LocalDateTime.now().minusYears(1);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        try {
            List<Map<String, Object>> statistics = reportService.getDriverWorkStatistics(driverId, startTime, endTime);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取驾驶员工作统计数据失败", e);
            return Result.error("获取驾驶员工作统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取违章统计数据
     */
    @GetMapping("/violation")
    public Result<List<Map<String, Object>>> getViolationStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        
        log.info("获取违章统计数据: year={}, month={}", year, month);
        
        try {
            List<Map<String, Object>> statistics = reportService.getViolationStatistics(year, month);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取违章统计数据失败", e);
            return Result.error("获取违章统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取事故统计数据
     */
    @GetMapping("/accident")
    public Result<List<Map<String, Object>>> getAccidentStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        
        log.info("获取事故统计数据: year={}, month={}", year, month);
        
        try {
            List<Map<String, Object>> statistics = reportService.getAccidentStatistics(year, month);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取事故统计数据失败", e);
            return Result.error("获取事故统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取加油统计数据
     */
    @GetMapping("/fuel")
    public Result<List<Map<String, Object>>> getFuelStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        
        log.info("获取加油统计数据: year={}, month={}", year, month);
        
        try {
            List<Map<String, Object>> statistics = reportService.getFuelStatistics(year, month);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取加油统计数据失败", e);
            return Result.error("获取加油统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取ETC费用统计数据
     */
    @GetMapping("/etc")
    public Result<List<Map<String, Object>>> getETCStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month,
            @RequestParam(required = false) Integer type) {
        
        log.info("获取ETC费用统计数据: year={}, month={}, type={}", year, month, type);
        
        try {
            List<Map<String, Object>> statistics = reportService.getETCStatistics(year, month, type);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取ETC费用统计数据失败", e);
            return Result.error("获取ETC费用统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取维修保养统计数据
     */
    @GetMapping("/maintenance")
    public Result<List<Map<String, Object>>> getMaintenanceStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month,
            @RequestParam(required = false) Integer type) {
        
        log.info("获取维修保养统计数据: year={}, month={}, type={}", year, month, type);
        
        try {
            List<Map<String, Object>> statistics = reportService.getMaintenanceStatistics(year, month, type);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取维修保养统计数据失败", e);
            return Result.error("获取维修保养统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取年检统计数据
     */
    @GetMapping("/inspection")
    public Result<List<Map<String, Object>>> getInspectionStatistics(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month,
            @RequestParam(required = false) Integer type) {
        
        log.info("获取年检统计数据: year={}, month={}, type={}", year, month, type);
        
        try {
            List<Map<String, Object>> statistics = reportService.getInspectionStatistics(year, month, type);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取年检统计数据失败", e);
            return Result.error("获取年检统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取车辆综合分析数据
     */
    @GetMapping("/vehicle/analysis/{vehicleId}")
    public Result<Map<String, Object>> getVehicleAnalysis(@PathVariable Long vehicleId) {
        log.info("获取车辆综合分析数据: vehicleId={}", vehicleId);
        
        try {
            Map<String, Object> analysis = reportService.getVehicleAnalysis(vehicleId);
            return Result.success(analysis);
        } catch (Exception e) {
            log.error("获取车辆综合分析数据失败", e);
            return Result.error("获取车辆综合分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取驾驶员综合分析数据
     */
    @GetMapping("/driver/analysis/{driverId}")
    public Result<Map<String, Object>> getDriverAnalysis(@PathVariable Long driverId) {
        log.info("获取驾驶员综合分析数据: driverId={}", driverId);
        
        try {
            Map<String, Object> analysis = reportService.getDriverAnalysis(driverId);
            return Result.success(analysis);
        } catch (Exception e) {
            log.error("获取驾驶员综合分析数据失败", e);
            return Result.error("获取驾驶员综合分析数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取费用分布统计数据
     */
    @GetMapping("/cost/distribution")
    public Result<List<Map<String, Object>>> getCostDistribution(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month) {
        
        log.info("获取费用分布统计数据: year={}, month={}", year, month);
        
        try {
            List<Map<String, Object>> distribution = reportService.getCostDistribution(year, month);
            return Result.success(distribution);
        } catch (Exception e) {
            log.error("获取费用分布统计数据失败", e);
            return Result.error("获取费用分布统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取总体运营成本分析
     */
    @GetMapping("/cost/operation")
    public Result<Map<String, Object>> getOperationCostAnalysis(
            @RequestParam(required = false) Integer year) {
        
        // 如果未提供年份，默认为当前年份
        if (year == null) {
            year = LocalDateTime.now().getYear();
        }
        
        log.info("获取总体运营成本分析: year={}", year);
        
        try {
            Map<String, Object> analysis = reportService.getOperationCostAnalysis(year);
            return Result.success(analysis);
        } catch (Exception e) {
            log.error("获取总体运营成本分析失败", e);
            return Result.error("获取总体运营成本分析失败: " + e.getMessage());
        }
    }
} 